package com.blank.web.dao;

import com.blank.web.bean.UserBean;
import com.blank.web.utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao{
    @Override
    public UserBean userLogin(UserBean user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1、获取数据连接
        try {
             connection = DbUtil.getConnection();
            System.out.println("数据库连接成功!");
            //2、准备sql语句
            String sql = "select * from t_user where userName = ? and password = ?";
            //3、执行sql语句获取结果并返回
             preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassword());
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                UserBean tub =  new UserBean(resultSet.getString("userName"),resultSet.getString("password"));
                return tub;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                DbUtil.closeResource(connection,preparedStatement);
                if (null != resultSet){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
